package controller

import (
	"blog/dao"
	"blog/models"
	"fmt"
	"github.com/gin-gonic/gin"
)

func AddUser(c *gin.Context) {
	username := c.PostForm("username")
	password := c.PostForm("password")
	user := models.User{
		Username: username,
		Password: password,
	}
	dao.Mgr.AddUser(&user)
	c.String(200, "添加新用户成功")

}

func ListUser(c *gin.Context) {
	c.HTML(200,"index.html", nil)
}

func RegisterUser(c *gin.Context) {
	username := c.PostForm("username")
	password := c.PostForm("password")
	user := models.User{
		Username: username,
		Password: password,
	}
	dao.Mgr.AddUser(&user)
	//c.Redirect(200, "/")
	c.HTML(200, "index.html", nil)

}

func GoRegister(c *gin.Context) {
	c.HTML(200, "register.html", nil)
}

func GoLogin(c *gin.Context) {
	c.HTML(200, "login.html", nil)
}

func Login(c *gin.Context) {
	username := c.PostForm("username")
	password := c.PostForm("password")
	fmt.Println(username)
	u := dao.Mgr.Login(username)
	if u.Username == "" {
		c.HTML(200, "login.html", "用户名不存在")
	} else {
		if u.Password != password {
			fmt.Println("密码错误")
			c.HTML(200, "login.html", "密码输入错误")
		} else {
			fmt.Println("登录成功")
			//c.Redirect(301, "/")
			c.HTML(200, "index.html", username)
		}
	}

}

func Index(c *gin.Context) {
	c.HTML(200, "index.html", "这是首页！！！！！！！")
}
